﻿using DevExpress.XtraGrid.Demos.util;
using DiaoPaiDaYin;
using DXApplicationTangche.DTO;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.model
{
    public class 款式配置设计点数据清洗Model
    {
        public List<DTO款式配置设计点数据清洗> DTO款式配置设计点数据清洗s = new List<DTO款式配置设计点数据清洗>();
        public 款式配置设计点数据清洗Model(List<DTO款式配置设计点展现> DTO款式配置设计点展现s)
        {
            DTO款式配置设计点展现s.Sort(new CComparer());
            //DTO款式配置设计点展现s.Sort((left, right) =>
            //{
            //    if (!left.P_ITEM_VALUE.Equals(right.P_ITEM_VALUE))
            //        return 0;
            //    else if (left.P_ITEM_VALUE.Equals(right.P_ITEM_VALUE))
            //        return 1;
            //    else
            //        return -1;
            //});
            String str = FunctionHelper.JsonSerialization(DTO款式配置设计点展现s);
            String P_ITEM_VALUE = "";
            DTO款式配置设计点数据清洗 DTO款式配置设计点数据清洗 = null;
            int i = 1;
            foreach (DTO款式配置设计点展现 DTO款式配置设计点展现 in DTO款式配置设计点展现s)
            {
                if (!P_ITEM_VALUE.Equals(DTO款式配置设计点展现.P_ITEM_VALUE))//新的第一个
                {
                    if (DTO款式配置设计点数据清洗 != null)
                    {
                        this.DTO款式配置设计点数据清洗s.Add(DTO款式配置设计点数据清洗);
                        DTO款式配置设计点数据清洗 = null;
                    }
                    DTO款式配置设计点数据清洗 = new DTO款式配置设计点数据清洗(DTO款式配置设计点展现);

                }
                else//第二个
                {
                    //  ADD
                    DTO款式配置设计点数据清洗.build设计点明细(DTO款式配置设计点展现);

                }
                //  新的大分组
                if (i== DTO款式配置设计点展现s.Count)
                {
                    this.DTO款式配置设计点数据清洗s.Add(DTO款式配置设计点数据清洗);
                }
                P_ITEM_VALUE = DTO款式配置设计点展现.P_ITEM_VALUE;
                i++;
                if(P_ITEM_VALUE==null)
                {
                    P_ITEM_VALUE = "";
                }
            }
        }
        public void UpdataVALUE(DTO款式配置设计点数据清洗 dtos)
        {
            String id = new Snowflake().GetId().ToString();
            String sql = "INSERT INTO `fragsmart-mtm`.`a_kuanshi_p` (\n" +
                                "	`id`,\n" +
                                "	`STYLEID`,\n" +
                                "	`STYLE_NAME_CN`,\n" +
                                "	`ITEM_VALUE`,\n" +
                                "	`ITEM_NAME_CN`,\n" +
                                "	`PARENT_ID`,\n" +
                                "	`PARENT_VALUE`,\n" +
                                "	`FILE_ID`,\n" +
                                "	`FLAG`,\n" +
                                "	`DEFAULT_CD`,\n" +
                                "	`DEFAULT_VALUE`,\n" +
                                "	`DEFAULT_NAME_CN`,\n" +
                                "	`PICN`,\n" +
                                "	`PICURL`,\n" +
                                "	`DEFAULT_FLAG` \n" +
                                ")\n" +
                                "VALUES\n" +
                                "	(\n" +
                                "		" + id + ",\n" +
                                "		" + dtos.STYLEID + ",\n" +
                                "		'" + dtos.STYLE_NAME_CN + "',\n" +
                                "		'" + dtos.P_ITEM_VALUE + "',\n" +
                                "		'" + dtos.P_ITEM_NAME_CN + "',\n" +
                                "		NULL,\n" +
                                "		NULL,\n" +
                                "		NULL,\n" +
                                "		'0',\n" +
                                "		'" + dtos.DEFAULT_CD + "',\n" +
                                "		'" + dtos.DEFAULT_VALUE + "',\n" +
                                "		'" + dtos.DEFAULT_NAME_CN + "',\n" +
                                "		NULL,\n" +
                                "		NULL,\n" +
                                "NULL \n" +
                                "	);";
            SQLmtm.ExecuteSql(sql);
            foreach(DTO款式配置设计点明细 dto in dtos.DTO款式配置设计点明细)
            {
                String sql1 = "INSERT INTO `fragsmart-mtm`.`a_kuanshi_p` (\n" +
                            "	`STYLEID`,\n" +
                            "	`STYLE_NAME_CN`,\n" +
                            "	`STYLE_CATEGORY_CD`,\n" +
                            "	`ITEM_CATEGORY_CD`,\n" +
                            "	`ITEM_CD`,\n" +
                            "	`ITEM_VALUE`,\n" +
                            "	`ITEM_NAME_CN`,\n" +
                            "	`PARENT_ID`,\n" +
                            "	`PARENT_VALUE`,\n" +
                            "	`FILE_ID`,\n" +
                            "	`FLAG`,\n" +
                            "	`DEFAULT_CD`,\n" +
                            "	`DEFAULT_VALUE`,\n" +
                            "	`DEFAULT_NAME_CN`,\n" +
                            "	`PICN`,\n" +
                            "	`PICURL`,\n" +
                            "	`DEFAULT_FLAG` \n" +
                            ")\n" +
                            "VALUES\n" +
                            "	(\n" +
                            "		NULL,\n" +
                            "		NULL,\n" +
                            "		NULL,\n" +
                            "		NULL,\n" +
                            "		'"+dtos.P_ITEM_VALUE+"',\n" +
                            "		'"+dto.C_ITEM_VALUE+"',\n" +
                            "		'"+dto.C_ITEM_NAME_CN+"',\n" +
                            "		"+id+",\n" +
                            "		NULL,\n" +
                            "		0,\n" +
                            "		NULL,\n" +
                            "		NULL,\n" +
                            "		NULL,\n" +
                            "		NULL,\n" +
                            "		'',\n" +
                            "		'',\n" +
                            "	0 \n" +
                            "	);";
                SQLmtm.ExecuteSql(sql1);
            }
        }
        public void UpdataVALUES()
        {
            foreach(DTO款式配置设计点数据清洗 dto in this.DTO款式配置设计点数据清洗s)
            {
                UpdataVALUE(dto);
            }
        }
    }

    public class CComparer : IComparer<DTO款式配置设计点展现>
    {
        public int Compare(DTO款式配置设计点展现 left, DTO款式配置设计点展现 right)
        {
            ////if (!left.P_ITEM_VALUE.Equals(right.P_ITEM_VALUE))
            //if (left.P_ITEM_VALUE != right.P_ITEM_VALUE)
            //    return 1;
            ////else if (left.P_ITEM_VALUE.Equals(right.P_ITEM_VALUE))
            //else if (left.P_ITEM_VALUE == right.P_ITEM_VALUE)
            //    return 0;
            //else
            //    return -1;
            return left.P_ITEM_VALUE.CompareTo(right.P_ITEM_VALUE);
        }
    }
}
